/*
 * stockexchange.h
 *
 *  Created on: 27/mag/2014
 *      Author: PegorarS
 */

#ifndef STOCKEXCHANGE_H_
#define STOCKEXCHANGE_H_

#include <vector>
#include "types.h"
#include "stock.h"
#include "portfolio.h"
#include "order.h"
using namespace std;

namespace NS_STOCKEXCHANGE{

	class SubscriberStockExchangeI{
	public:
		virtual ~SubscriberStockExchangeI() = 0;

		virtual void Begin(Ticker stockSeriesName) = 0;
		virtual void Update(StockData &stockData) = 0;
	};

	class PublisherStockExchangeI{
	public:
		virtual ~PublisherStockExchangeI() = 0;

		void Attach(SubscriberStockExchangeI *obs){
			subs.push_back(obs);
		}

		void StartSeries(Ticker stockSeriesName){
			vector<class SubscriberStockExchangeI *>::iterator i;

			for(i = subs.begin(); i <= subs.end(); i++)
				(*i)->Begin(stockSeriesName);
		}

		void Publish(StockData stockData){
			vector<class SubscriberStockExchangeI *>::iterator i;

			for(i = subs.begin(); i <= subs.end(); i++)
				(*i)->Update(stockData);
		}

		virtual void CommitOrder(Order *pOrder, Portfolio *pTargetPortfolio = NULL) = 0;

		void Detach(SubscriberStockExchangeI *subToRemove){
			for(unsigned int i= 0; i< subs.size(); i++){
				if (subs[i]== subToRemove){
					subs.erase(subs.begin()+i);
					return;
				}
			}
		}

	private:
		vector<class SubscriberStockExchangeI*> subs;
	};

	class StockDbBrokerI{
	public:
		virtual ~StockDbBrokerI() = 0;

		virtual bool GetStockData(Ticker stockName, Date stockDate, StockData &data) = 0;
	};

}



#endif /* STOCKEXCHANGE_H_ */
